package ao.test;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.*;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 */
public class Main
{
    public static void main(String[] args) throws IOException {
        Collection<File> javaFiles = FileUtils.listFiles(
                new File("C:/~/proj/holdem/src"),
                new String[]{"java"},
                true);

        for (File javaFile : javaFiles) {
//            File javaFile = new File("C:/~/temp/CfrBot2.java");
            System.out.println("Parsing: " + javaFile);

            char[] fileChars = Files.toString(javaFile, Charsets.UTF_8).toCharArray();

            ASTParser parser = ASTParser.newParser(AST.JLS3);  // handles JDK 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6
            parser.setSource(fileChars);


            // In order to parse 1.5 code, some compiler options need to be set to 1.5
//            Map options = JavaCore.getOptions();
//            JavaCore.setCom
//            JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
//            parser.setCompilerOptions(options);

            CompilationUnit result = (CompilationUnit) parser.createAST(null);
            process(result);
        }
    }

    private static void process(final CompilationUnit cu) {
        cu.accept(new ASTVisitor() {

            public boolean visit(MethodDeclaration node) {
                List<String> annotations = Lists.newArrayList();

                List<?> modifiers = node.modifiers();
                for (Object modifier : modifiers) {
                    if (modifier instanceof Annotation) {
                        annotations.add(((Annotation) modifier).getTypeName().toString());
                    }
                }

                if (annotations.size() > 0) {
                    System.out.println(node.getName() + "\t\t" + Joiner.on(" | ").join(annotations));
                }
                return true;
            }
        });
    }
}
